/*****************************************************************************************
  Project   : The Impact of Unions on Non-union Wage Setting: Threats and Bargaining
  Authors   : David A. Green, Ben M. Sand, Iain G. Snoddy, Jeanne Tschopp
  Date      : August 2025
  Output    : Data for linked Morg to create transitions
 *****************************************************************************************/


/*
IPUMS MORG data, linked.

*/

clear
set more off
set type double, perm

local keepvars age detind union  sex wgt race year rawwage ind uhrswork1  classwkr  paidhour educ female

// May matches first

use ${wd}/May_matching/may_ipums_match_final.dta, clear


	keep if inrange(empstat, 10,12)
	keep if inrange(classwkr, 21, 28)
	
// clean up some vars
	// union
	rename union_mem union

	// demographics
	gen female = sex == 2
	gen x = 1 if educ <=60
	replace x = 2 if inrange(educ, 71,73)
	replace x = 3 if inrange(educ, 80,100)
	replace x = 4 if inrange(educ, 110, 125)
	drop educ
	rename x educ
	
	// hourly pay
	rename paidhr  paidhour

// GSave 2nd period
	keep if mish>4

keep `keepvars' cpsidp	mish
renvars `keepvars', postfix(_2)

save ${temp}/temp.dta,replace

*use  ${raw}/ipums_cps_matching/may_ipums_match_final.dta,clear

use ${wd}/May_matching/may_ipums_match_final.dta, clear

	keep if inrange(empstat, 10,12)
	keep if inrange(classwkr, 21, 28)


// clean up some vars
	// union
	rename union_mem union

	// demographics
	gen female = sex == 2
	gen x = 1 if educ <=60
	replace x = 2 if inrange(educ, 71,73)
	replace x = 3 if inrange(educ, 80,100)
	replace x = 4 if inrange(educ, 110, 125)
	drop educ
	rename x educ
	
	// hourly pay
	rename paidhr  paidhour
		
// Save 1st period
	keep if mish<=4

	keep `keepvars' cpsidp mish 
	replace mish=mish+4
	renvars `keepvars', postfix(_1)

// Get matches from periods 1 to 2
	merge 1:1 cpsidp mish using ${temp}/temp.dta
	keep if _merge==3
	drop _merge

// save for time 1	
	gen year = 1980
	rename wgt_1 earnwt_1


// keep only varified matches
	gen byte sexdif=sex_1==sex_2
	gen byte racedif=race_1==race_2
	gen byte agedif=age_2-age_1
	gen byte ragedif=(agedif==0 | agedif==1 | agedif==2)
	gen byte nragedif=(agedif==-1 | agedif==0 | agedif==1 | agedif==2 | agedif==3)

	gen match = 1
	replace match = 0 if (sexdif!=1 | racedif != 1 | ragedif != 1)
	tab match
	keep if match == 1

// save period 1 matches
save ${wd}/ipums_ind_trans/matches81.dta,replace


// Getting matches for Morg
// Morg matches
local keepvars age detind union  earnwt sex  mis race earnweek paidhour hourwage ind classwkr uhrswork1  educ female wtfinl 
tempfile temp

foreach x of num 1989  1999 2009 2019 {  //
		
	
		
	if `x'==1989 {
		local l = 1988
		local h = 1991
	} 
	else if `x' == 1999 {
		local l = 1998
		local h = 2001
	} 
	else if `x' == 2009 {
		local l = 2008
		local h = 2011
	} 
	else if `x' == 2019 {
		local l = 2017
		local h = 2020
	}
	
	use "${wd}/ipums_ind_trans/IPUMS_MORG_88-20.dta" if inrange(year, `l', `h'), clear
	
	keep if inrange(empstat, 10,12)
	keep if inrange(classwkr, 21, 28)
	

// clean up some vars
	// demographics
	gen female = sex == 2
	gen x = 1 if educ <=60
	replace x = 2 if inrange(educ, 71,73)
	replace x = 3 if inrange(educ, 80,100)
	replace x = 4 if inrange(educ, 110, 125)
	drop educ
	rename x educ

	// industry codes
	g ind_90=ind1990
	do ${cps}/programs/ind_90_des_crosswalk
	do ${cps}/programs/ind_des_major_recodes

	// keep matches		
	keep if mis==4 | mis==8
	duplicates tag cpsidp,g(dups)
	keep if dups==1
	drop dups
	
	keep `keepvars' cpsidp 
	recode union (1=0) (2/3=1)
	gen year = `x'
	
	preserve
		keep if mis==4
		renvars `keepvars', postfix(_1)

		save ${temp}/temp.dta,replace
	restore

	keep if mis==8
	renvars `keepvars', postfix(_2)
	
	merge 1:1 cpsidp using ${temp}/temp.dta
	tab _merge
	drop if _merge!=3
	drop _merge
	
	label drop union_lbl

	save ${wd}/ipums_ind_trans/matches`x'.dta,replace
	
	 cap append using `temp'
	 save `temp', replace
	
}



// keep only varified matches
	gen byte sexdif=sex_1==sex_2
	gen byte racedif=race_1==race_2
	gen byte agedif=age_2-age_1
	gen byte ragedif=(agedif==0 | agedif==1 | agedif==2)
	gen byte nragedif=(agedif==-1 | agedif==0 | agedif==1 | agedif==2 | agedif==3)

	gen match = 1
	replace match = 0 if (sexdif!=1 | racedif != 1 | ragedif != 1)
	tab match
	keep if match == 1

// add may matches
append using ${wd}/ipums_ind_trans/matches81.dta
tab year


// sample after matching
keep if age_1 >= 20
keep if age_2 <= 66

keep if inlist(classwkr_1, 21, 22, 23, 24, 25)
keep if inlist(classwkr_2, 21, 22, 23, 24, 25)

egen job   = group(detind_1 union_1)
egen job_2 = group(detind_2 union_2)

gen flag_1 = ind_1 != ind_2 if job==job_2 & ind_1 > 0 & ind_2 > 0
gen flag_2 = paidhour_1 != paidhour_2 if job==job_2 & inlist(paidhour_1, 1,2) &inlist(paidhour_2,1,2)
gen flag_3 = classwkr_1 !=  classwkr_2 if job==job_2

egen flag_t = rowtotal(flag_*) if job==job_2

gen switch = flag_t >= 1 if job==job_2

drop if (job==job_2 & switch == 0)

// year variable to match main data.
gen year2=0
replace year2=1 if year>=1978 & year<=1980
replace year2=2 if year>=1988 & year<=1990
replace year2=3 if year>=1998 & year<=2000
replace year2=4 if year>=2008 & year<=2010
replace year2=5 if year>=2018 & year<=2020


rename earnwt_1 wgt_1

// rename match period 1 demographis for later sample restrictions
renvars age_1 female_1 educ_1 , postdrop(2)

// Final clean up
drop if detind_1 == .
drop if detind_2 == .
drop if union_1 == . | union_2 == .


// save 
lab data "Industry Transitions from IPUMS CPS"
save  ${wd}/ipums_ind_trans/matched_cps.dta, replace

